import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-05-19
 * Time: 13:06
 */
class Solution {
    public static List<List<Integer>> fourSum(int[] nums, int target) {
        //1.排序
        Arrays.sort(nums);

        List<List<Integer>> list = new ArrayList<>();
        int n = nums.length;
        for (int i = 0; i < n - 3;) {//固定a
            for (int j = i + 1; j < n - 2;) {//固定b
                //2.双指针求解
                int left = j + 1;
                int right = n - 1;
                while (left < right) {
                    long add = (long)target - nums[i] - nums[j];
                    if (nums[left] + nums[right] > add) {
                        right--;
                    }else if (nums[left] + nums[right] < add) {
                        left++;
                    }else {
                        list.add(new ArrayList<>(Arrays.asList(nums[i], nums[j], nums[left], nums[right])));
                        //继续缩小查询
                        left++;
                        right--;

                        //去重
                        while (left < right && nums[left] == nums[left - 1]) {
                            left++;
                        }

                        while (left < right && nums[right] == nums[right + 1]) {
                            right--;
                        }
                    }
                }

                j++;
                while (j < n - 2 && nums[j] == nums[j - 1]) {
                    j++;
                }
            }

            i++;
            while (i < n - 3 && nums[i] == nums[i - 1]) {
                i++;
            }
        }

        return list;
    }


   /* public List<List<Integer>> fourSum(int[] nums, int target) {
        //1.排序
        Arrays.sort(nums);

        HashSet<List<Integer>> set = new  HashSet<List<Integer>>();//set去重
        int n = nums.length;
        for (int i = 0; i < n - 3;) {//固定a
            for (int j = i + 1; j < n - 2;) {//固定b
                //2.双指针求解
                int left = j + 1;
                int right = n - 1;
                while (left < right) {
                    long add = (long)target - nums[i] - nums[j];
                    if (nums[left] + nums[right] > add) {
                        right--;
                    }else if (nums[left] + nums[right] < add) {
                        left++;
                    }else {
                        set.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        //继续缩小查询
                        left++;
                        right--;
                    }
                }

                j++;
                while (j < n - 2 && nums[j] == nums[j - 1]) {
                    j++;
                }
            }

            i++;
            while (i < n - 3 && nums[i] == nums[i - 1]) {
                i++;
            }
        }

        return new ArrayList<>(set);
    }
    */
    public static void main(String[] args) {
        int target = -294967296;
        int[] nums = {1000000000,1000000000,1000000000,1000000000};
        fourSum(nums, target);
    }
}
